
package server;

import DatagramPlusPlus.DatagramPlusPlusSocket;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 *
 * @author rafael
 */
public class Servidor implements Runnable{

    private DatagramPlusPlusSocket socket;

    public Servidor(DatagramPlusPlusSocket socket)  {
        this.socket = socket;
    }

    public void run() {
        InputStream in      = socket.getInputStream();
        OutputStream out    = socket.getOutputStream();

//        BufferedReader stdin = new BufferedReader(new FileReader(System.in));
//        System.out.println("Insira o caminho para o ficheiro");
//        String ficheiro = stdin.readLine();
        
        File outputfile = new File("output");
        try {
            OutputStream output = new FileOutputStream(outputfile);
            byte[] buf = new byte[1034];
            in.read(buf);
            while(socket.notClosed()) {
                output.write(buf);
                in.read(buf);
            }
        } catch (IOException ex) {
            System.err.println("Servidor:Impossivel criar/escrever no ficheiro.");
//        } catch (FileNotFoundException ex) {
//            System.err.println("Servidor:Impossivel criar/escrever ficheiro.");
        }
        
    }


}
